<?php

namespace App\Admin\Actions\Grid\Withdraw;

use App\Models\CheckMint;
use App\Models\UserDeposit;
use App\Models\UserWithdraw;
use App\Services\Base\CacheService;
use App\Services\BlockChainServices;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\RowAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class WithdrawSuccessGrid extends RowAction
{
    /**
     * @return string
     */
    protected $title = '确认完成';


    public function getConfirmContent()
    {
        return "请先确认链上是成功的，成功之后点击确认";
    }

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        Log::debug("提现手动确认状态成功:" . $this->getKey());
        /**
         * @var $user_withdraw UserWithdraw
         */
        $user_withdraw = UserWithdraw::query()->where('id', $this->getKey())->first();
        BlockChainServices::checkWithdraw(['tx_id' => $user_withdraw->executed_tx, 'timestamp' => time()]);
        return $this->success();
    }

    public function success()
    {
        return $this->response()
            ->success('操作成功: 请稍后查询结果')
            ->redirect('/user-withdraw');
    }

    public function fail()
    {
        return $this->response()
            ->error('操作失败: ' . $this->getKey())
            ->redirect('/user-withdraw');
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return [$this->getConfirmContent(), '弹窗内容'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [
        ];
    }

}
